package com.woniuxy.book87.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.book87.entity.Permission;
import com.woniuxy.book87.result.Result;
import com.woniuxy.book87.service.PermissionService;
import com.woniuxy.book87.vo.PermissionMenuVo;
import com.woniuxy.book87.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService ;

    @GetMapping("/menuList")
    public Result<List<PermissionMenuVo>> meunList(){
        try {
            List<PermissionMenuVo> allMenu = permissionService.findAllMenu();
            return Result.ok(allMenu);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return  Result.fail();
        }
    }

    @GetMapping("/list")
    public Result<PageInfo<PermissionVo>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "3") Integer size,
            String name){
        try {
            PageInfo<PermissionVo> pageInfo = permissionService.findPermissionByPage(now,size,name);
            return Result.ok("查询菜单成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询菜单失败!");
        }
    }

    @GetMapping("/getAllParentMenu")
    public Result<List<PermissionMenuVo>> getAllParentMenu(){
        try {
            List<PermissionMenuVo> lists = permissionService.findAllParentMenu();
            return Result.ok("查询父菜单成功",lists);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询父菜单失败!");
        }
    }
    @PostMapping("/add")
    public Result<String> add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return Result.ok("保存菜单成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("保存菜单失败!");
        }
    }

    @GetMapping("/getPermissionByKey/{id}")
    public Result<Permission> getPermissionByKey(@PathVariable("id") Long id){
        try {
            Permission permission = permissionService.findPermissionByKey(id);
            return Result.ok("查询成功",permission);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败!");
        }
    }

    @PutMapping("/edit")
    public Result<String> edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return Result.ok("修改菜单成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("修改菜单失败!");
        }
    }

    @DeleteMapping("/delPermissionByKey/{id}")
    public Result<String> delPermissionByKey(@PathVariable("id") Long id){
        try {
            permissionService.removePermissionByKey(id);
            return Result.ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("删除失败!");
        }
    }


}
